import React, { useRef } from 'react';
import CustomForm from 'src/components/CustomForm';
import { Input, message } from 'antd';
import services from 'src/services/api';

export default function Publish(props) {
  const form = useRef(null);
  const formItems = [
    {
      label: {
        label: '模型服务名称',
        className: 'tantuer-form-item tantuer-form-item-nowrap',
      },
      name: 'server_name',
      options: {
        initialValue: '',
        rules: [
          { required: true, message: '请填写模型服务名称' },
          { max: 128, message: '最长128个字符' },
          { pattern: /^[a-zA-Z][_a-zA-Z0-9]*$/, message: '限英文数字下划线，并以英文开头' },
        ],
      },
      component: <Input placeholder='请填写' style={{ width: '300px' }} />,
      after: <Input value={props.dataSource.model_version} disabled />,
    },
    // {
    //   label: {
    //     label: '运行环境',
    //     className: 'tantuer-form-item tantuer-form-item-nowrap',
    //   },
    //   name: 'runtime_env',
    //   options: {
    //     initialValue: '',
    //     rules: [{ required: true, message: '请选择运行环境' }],
    //   },
    //   component: (
    //     <Select style={{ width: '300px' }}>
    //       {MODEL_ENV.map((it) => (
    //         <Select.Option value={it.value}>{it.text}</Select.Option>
    //       ))}
    //     </Select>
    //   ),
    // },
  ];

  function handleSubmit() {
    form.current.validateFields((err, values) => {
      if (!err) {
        // const params = {
        //   server_name: values.server_name,
        //   model_id: props.dataSource.model_id,
        // };
        const params = {
          model_code: props.dataSource.model_code,
          model_version: props.dataSource.model_version,
          deploy_name: values.server_name,
        };
        services.repositoryModel.deploy(params).then((data) => {
          if (data.code === 0) {
            message.success(data.msg);
            if (props.closeModal) {
              props.closeModal();
              props.history.push('/variable/model/config');
            }
          } else {
            message.error(data.msg);
          }
        });
      }
    });
  }

  return (
    <CustomForm
      closeModal={props.closeModal}
      createForm={(ref) => (form.current = ref)}
      formItems={formItems}
      colNum={1}
      onHandleSubmit={handleSubmit}
    />
  );
}
